﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GenIX.StyleSpace;
namespace GenIX
{
    public partial class NewStyleForm : Form
    {

      
        public void ChangeStyle(string stylename)
        {
           
            temp = (Style)Styleform.getStyle(stylename).Clone();
            button = new SButton();
            form = new SForm();
            edit = new SEdit(); ;
            label = new SLabel();
            menu = new SMenu();

            if (temp.sbutton.check)
            {
                this.button = temp.sbutton;
                listBox1.Items.Add("кнопка");
            }

            if (temp.slabel.check)
            {
                this.label = temp.slabel;
                listBox1.Items.Add("поле");
            }

            if (temp.sedit.check)
            {
                this.edit = temp.sedit;
                listBox1.Items.Add("поле ввода");
            }

            if (temp.sform.check)
            {
                this.form = temp.sform;
                listBox1.Items.Add("форма");
            }

            if (temp.smenu.check)
            {
                this.menu = temp.smenu;
                listBox1.Items.Add("меню");
            }
            this.groupBox1.Text = "Список элементов стиля";
            this.Text = "Редактирование стиля: " + temp.name;
            if (listBox1.Items.Count > 0)
            {
                listBox1.SelectedItem = listBox1.Items[0];
                currentStyle = listBox1.Items[0].ToString();
                Bilding(listBox1.Items[0].ToString(), true);

            }
        }
      
        public void InitStyle(string name)
        {
            temp = new StyleSpace.Style(name);
            this.groupBox1.Text = "Список элементов стиля";
            this.Text = "Создание нового стиля: " + name;
            currentStyle = "";
            button = new SButton();
            form = new SForm();
            edit = new SEdit(); ;
            label = new SLabel();
            menu = new SMenu();
        }

      
        private void button3_Click(object sender, EventArgs e)
        {
            NewTypeForm form = new NewTypeForm();
            form.IniList(listStile);
            form.ShowDialog();
            if (!form.select) return;
            string item = form.comboBox1.SelectedItem.ToString();
            if (item == "") form.ShowDialog();

            for (int i = 0; i < listBox1.Items.Count; i++)
                if (listBox1.Items[i].ToString() == item)
                {
                    MessageBox.Show("Элемент выбран,выберети другой");
                    return;
                }
            listBox1.Items.Add(item);

           
            if (currentStyle != "") ReBilding(currentStyle, item);
            currentStyle = item;
            listBox1.SelectedItem = item;
            Bilding(item);
        }
      
        private void ClickItem(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                Bilding(listBox1.SelectedItem.ToString());
            }
        }
      
        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null) return;
            panel1.Controls.Clear();
            switch (listBox1.SelectedItem.ToString())
            {
                case "кнопка":
                    button = new SButton();
                    break;
                case "форма":
                    form = new SForm();
                    break;
                case "поле ввода":
                    edit = new SEdit(); ;
                    break;
                case "поле":
                    label = new SLabel();
                    break;
                case "меню":
                    menu = new SMenu();
                    break;
            }
            listBox1.Items.Remove(listBox1.SelectedItem);
            if (listBox1.Items.Count > 0)
            {
                listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count - 1];
                Bilding(listBox1.SelectedItem.ToString());
                currentStyle = listBox1.SelectedItem.ToString();
            }
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
     
        private void Bilding(string name, bool newelement = false)
        {
         
            switch (name)
            {
                case "кнопка":
                    BButton(newelement);
                    break;
                case "поле":
                    BLabel(newelement);
                    break;
                case "форма":
                    BForm(newelement);
                    break;
                case "поле ввода":
                    BEdit(newelement);
                    break;
                case "меню":
                    BMenu(newelement);
                    break;
            }
        }
     
        private void ReBilding(string nameType, string newnameType)
        {
            if (button.check == true && nameType == "кнопка")
            {
                button.ColorT = color.SelectedItem.ToString().Trim();
                button.ForeColorT = forecolor.SelectedItem.ToString().Trim();
                button.FontSizeT = sizefont.Value.ToString();
                button.FontFamalyT = font.SelectedItem.ToString().Trim();
                button.FontStyleT = fontStyle.SelectedItem.ToString().Trim();
                currentStyle = newnameType;
            }
            if (label.check == true && nameType == "поле")
            {
                label.ColorT = color.SelectedItem.ToString().Trim();
                label.ForeColorT = forecolor.SelectedItem.ToString().Trim();
                label.FontSizeT = sizefont.Value.ToString();
                label.FontFamalyT = font.SelectedItem.ToString().Trim();
                label.FontStyleT = fontStyle.SelectedItem.ToString().Trim();
                currentStyle = newnameType;
            }
            if (form.check == true && nameType == "форма")
            {
                form.ColorT = color.SelectedItem.ToString().Trim();
                form.ForeColorT = forecolor.SelectedItem.ToString().Trim();
                form.FontSizeT = sizefont.Value.ToString();
                form.FontFamalyT = font.SelectedItem.ToString().Trim();
                form.FontStyleT = fontStyle.SelectedItem.ToString().Trim();
                currentStyle = newnameType;
            }
            if (edit.check == true && nameType == "поле ввода")
            {
                edit.ColorT = color.SelectedItem.ToString().Trim();
                edit.ForeColorT = forecolor.SelectedItem.ToString().Trim();
                edit.FontSizeT = sizefont.Value.ToString();
                edit.FontFamalyT = font.SelectedItem.ToString().Trim();
                edit.FontStyleT = fontStyle.SelectedItem.ToString().Trim();
                currentStyle = newnameType;
            }
            if (menu.check == true && nameType == "меню")
            {
                menu.ColorT = color.SelectedItem.ToString().Trim();
                menu.ForeColorT = forecolor.SelectedItem.ToString().Trim();
                menu.FontSizeT = sizefont.Value.ToString();
                menu.FontFamalyT = font.SelectedItem.ToString().Trim();
                menu.FontStyleT = fontStyle.SelectedItem.ToString().Trim();
                currentStyle = newnameType;
            }


        }
       
        private void BButton(bool newlement = false)
        {
            if (newlement != true)
                ReBilding(currentStyle, "кнопка");
           
            this.button.check = true;
            panel1.Controls.Clear();
            this.groupBox2.Text = "Параметры стиля: кнопка";
            template();
            font.SelectedItem = this.button.FontFamalyT;
            color.SelectedItem = "     " + this.button.ColorT;
            forecolor.SelectedItem = "     " + this.button.ForeColorT;
            fontStyle.SelectedItem = this.button.FontStyleT;
            sizefont.Value = Int32.Parse(this.button.FontSizeT);
        }
        private void BLabel(bool newlement = false)
        {
            if (newlement != true)
               
                ReBilding(currentStyle, "поле");
            this.label.check = true;
            panel1.Controls.Clear();
            this.groupBox2.Text = "Параметры стиля: поле";
            template();
            font.SelectedItem = this.label.FontFamalyT;
            color.SelectedItem = "     " + this.label.ColorT;
            forecolor.SelectedItem = "     " + this.label.ForeColorT;
            fontStyle.SelectedItem = this.label.FontStyleT;
            sizefont.Value = Int32.Parse(this.label.FontSizeT);
        }
        private void BMenu(bool newlement = false)
        {
            if (newlement != true)
             
                ReBilding(currentStyle, "меню");
            this.menu.check = true;
            panel1.Controls.Clear();
            this.groupBox2.Text = "Параметры стиля: меню";
            template();
            font.SelectedItem = this.menu.FontFamalyT;
            color.SelectedItem = "     " + menu.ColorT;
            forecolor.SelectedItem = "     " + menu.ForeColorT;
            fontStyle.SelectedItem = menu.FontStyleT;
            sizefont.Value = Int32.Parse(menu.FontSizeT);
        }
        private void BForm(bool newlement = false)
        {
            if (newlement != true)
              
                ReBilding(currentStyle, "форма");
            this.form.check = true;
            panel1.Controls.Clear();
            this.groupBox2.Text = "Параметры стиля: форма";
            template();
            font.SelectedItem = this.form.FontFamalyT;
            color.SelectedItem = "     " + form.ColorT;
            forecolor.SelectedItem = "     " + form.ForeColorT;
            fontStyle.SelectedItem = form.FontStyleT;
            sizefont.Value = Int32.Parse(form.FontSizeT);
        }
        private void BEdit(bool newlement = false)
        {
            if (newlement != true)
              
                ReBilding(currentStyle, "поле ввода");
            this.edit.check = true;
            panel1.Controls.Clear();
            this.groupBox2.Text = "Параметры стиля: поле ввода";
            template();
            font.SelectedItem = this.edit.FontFamalyT;
            color.SelectedItem = "     " + edit.ColorT;
            forecolor.SelectedItem = "     " + edit.ForeColorT;
            fontStyle.SelectedItem = edit.FontStyleT;
            sizefont.Value = Int32.Parse(edit.FontSizeT);
        }
        private void button5_Click(object sender, EventArgs e)
        {
        }
     
        private void button1_Click(object sender, EventArgs e)
        {
            if (mood == true)
            {

                ReBilding(currentStyle, currentStyle);
                temp.sbutton = button;
                temp.sedit = edit;
                temp.smenu = menu;
                temp.slabel = label;
                temp.sform = form;
                Styleform.AddStyle(temp);
            }
            else
            {
                ReBilding(currentStyle, currentStyle);
                temp.sbutton = button;
                temp.sedit = edit;
                temp.smenu = menu;
                temp.slabel = label;
                temp.sform = form;
                Styleform.ChangeStyle(temp);
            }
            Close();
        }
        private void NewStyleForm_Load(object sender, EventArgs e)
        {
            Styleform.SelectElement(this, Styleform.choseStyleName);
            if (listBox1.Items.Count > 0)
                Bilding(listBox1.Items[0].ToString(), true);
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
       
        private void template()
        {
            color = new ComboBox();
            forecolor = new ComboBox();
            font = new ComboBox();
            fontStyle = new ComboBox();
            sizefont = new NumericUpDown();
            textcolor = new Label();
            textforecolor = new Label();
            textfontstyle = new Label();
            textfont = new Label();
            textsizefont = new Label();

            textcolor.Text = "Цвет фона: ";
            textforecolor.Text = "Цвет шрифта: ";
            textfont.Text = "Вид шрифта: ";
            textfontstyle.Text = "Стиль шрифта: ";
            textsizefont.Text = "Размер шрифта: ";

            color.DrawMode = DrawMode.OwnerDrawVariable;
            color.Items.AddRange(colorString);
            color.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.Draw);
            color.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

            forecolor.DrawMode = DrawMode.OwnerDrawVariable;
            forecolor.Items.AddRange(colorString);
            forecolor.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.Draw);
            forecolor.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

            font.DrawMode = DrawMode.OwnerDrawVariable;
            font.Items.AddRange(listFont);
            font.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.DrawFont);
            font.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

            fontStyle.DrawMode = DrawMode.OwnerDrawVariable;
            fontStyle.Items.AddRange(listStyleFont);
            fontStyle.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.DrawStyle);
            fontStyle.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;


            textfont.Location = new System.Drawing.Point(32, 20);
            font.Location = new System.Drawing.Point(150, 20);



            textcolor.Location = new System.Drawing.Point(32, 50);
            color.Location = new System.Drawing.Point(150, 50);


            textforecolor.Location = new System.Drawing.Point(32, 80);
            forecolor.Location = new System.Drawing.Point(150, 80);


            textfontstyle.Location = new System.Drawing.Point(32, 110);
            fontStyle.Location = new System.Drawing.Point(150, 110);


            textsizefont.Location = new System.Drawing.Point(32, 140);
            sizefont.Location = new System.Drawing.Point(150, 140);

            sizefont.Minimum = 5;
            sizefont.Maximum = 15;

            panel1.Controls.Add(color);
            panel1.Controls.Add(forecolor);

            panel1.Controls.Add(textcolor);
            panel1.Controls.Add(textforecolor);

            panel1.Controls.Add(font);
            panel1.Controls.Add(textfont);

            panel1.Controls.Add(fontStyle);
            panel1.Controls.Add(textfontstyle);

            panel1.Controls.Add(sizefont);
            panel1.Controls.Add(textsizefont);
        }
      
        private void Draw(object sender, DrawItemEventArgs e)
        {
        
            e.DrawBackground();
            Brush myBrush = Brushes.Black;
           

            switch (e.Index)
            {
                case 0:
                    myBrush = Brushes.Tan;
                    break;
                case 1:
                    myBrush = Brushes.Black;
                    break;
                case 2:
                    myBrush = Brushes.Gainsboro;
                    break;
                case 3:
                    myBrush = Brushes.Gray;
                    break;
                case 4:
                    myBrush = Brushes.Red;
                    break;
                case 5:
                    myBrush = Brushes.WhiteSmoke;
                    break;
                case 6:
                    myBrush = Brushes.LemonChiffon;
                    break;
                case 7:
                    myBrush = Brushes.Pink;
                    break;
                case 8:
                    myBrush = Brushes.Blue;
                    break;
                case 9:
                    myBrush = Brushes.LightSteelBlue;
                    break;
                case 10:
                    myBrush = Brushes.AliceBlue;
                    break;
                case 11:
                    myBrush = Brushes.Green;
                    break;
                case 12:
                    myBrush = Brushes.Aquamarine;
                    break;
                case 13:
                    myBrush = Brushes.Yellow;
                    break;
                case 14:
                    myBrush = Brushes.DarkSalmon;
                    break;
                default: myBrush = Brushes.Black;
                    break;

            }
            var a1 = (e.Bounds);
            a1.Width = a1.Width - 85;
            e.Graphics.FillRectangle(myBrush, (Rectangle)a1);
            Font a = new System.Drawing.Font(e.Font.FontFamily, 8, FontStyle.Regular);

            e.Graphics.DrawString(((ComboBox)sender).Items[e.Index].ToString(), a, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
            e.DrawFocusRectangle();
        }
     
        private void DrawFont(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();

        
            Brush myBrush = Brushes.Black;

            Font a = new System.Drawing.Font(e.Font.FontFamily, 8, FontStyle.Bold);
            switch (e.Index)
            {
                case 0:
                    a = new System.Drawing.Font("Segoe Script", 8, FontStyle.Regular);
                    break;
                case 1:
                    a = new System.Drawing.Font("Segoe Print", 8, FontStyle.Regular);
                    break;
                case 2:
                    a = new System.Drawing.Font("Segoe UI", 8, FontStyle.Regular);
                    break;
                case 3:
                    a = new System.Drawing.Font("Segoe UI Symbol", 8, FontStyle.Regular);
                    break;
                case 4:
                    a = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
                    break;
                case 5:
                    a = new System.Drawing.Font("Times New Roman", 8, FontStyle.Regular);
                    break;

                case 6:
                    a = new System.Drawing.Font("Trebuchet MS", 8, FontStyle.Regular);
                    break;
                case 7:
                    a = new System.Drawing.Font("Tahoma", 8, FontStyle.Regular);
                    break;
                case 8:
                    a = new System.Drawing.Font("Courier New", 8, FontStyle.Regular);
                    break;
                case 9:
                    a = new System.Drawing.Font("Franklin Gothic", 8, FontStyle.Regular);
                    break;
                case 10:
                    a = new System.Drawing.Font("Microsoft Sans Serif", 8, FontStyle.Regular);
                    break;

            }

            e.Graphics.DrawString(((ComboBox)sender).Items[e.Index].ToString(), a, myBrush, e.Bounds, StringFormat.GenericDefault);
            e.DrawFocusRectangle();
        }
      
        private void DrawStyle(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            Brush myBrush = Brushes.Black;
            Font a = new System.Drawing.Font(e.Font.FontFamily, 8, FontStyle.Bold);
            switch (e.Index)
            {
                case 0:
                    a = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
                    break;
                case 1:
                    a = new System.Drawing.Font("Arial", 8, FontStyle.Bold);
                    break;
                case 2:
                    a = new System.Drawing.Font("Arial", 8, FontStyle.Italic);
                    break;
                case 3:
                    a = new System.Drawing.Font("Arial", 8, FontStyle.Strikeout);
                    break;
                case 4:
                    a = new System.Drawing.Font("Arial", 8, FontStyle.Underline);
                    break;
            }

            e.Graphics.DrawString(((ComboBox)sender).Items[e.Index].ToString(), a, myBrush, e.Bounds, StringFormat.GenericDefault);
            e.DrawFocusRectangle();
        }
       

        public bool mood = false;
        Style temp;
        SButton button;
        SLabel label;
        SEdit edit;
        SForm form;
        SMenu menu;
        string[] listStyleFont;
        string[] listStile;
        string[] listFont;
        string[] colorString;
        string currentStyle;
        ComboBox color;
        ComboBox font;
        ComboBox forecolor;
        ComboBox fontStyle;
        NumericUpDown sizefont;
        Label textcolor;
        Label textforecolor;
        Label textfontstyle;
        Label textfont;
        Label textsizefont;
      
        public NewStyleForm()
        {
            InitializeComponent();
            listStile = new string[]
            {
               "кнопка",
               "форма",
               "поле ввода",
               "поле",
               "меню"
            };
            colorString = new string[] 
            {
                "     Tan",
                "     Black",
                "     Gainsboro",
                "     Gray",
                "     Red",
                "     WhiteSmoke",
                "     LemonChiffon",
                "     Pink",
                "     Blue",
                "     LightSteelBlue",
                "     AliceBlue",
                "     Green",
                "     Aquamarine",
                "     Yellow",
                "     DarkSalmon"
            };
            listFont = new string[] 
            {
                   "Segoe Script",
                   "Segoe Print",
                   "Segoe UI",
                   "Segoe UI Symbol",
                   "Arial",
                   "Times New Roman",
                   "Trebuchet MS",
                   "Tahoma",
                   "Courier New",
                   "Franklin Gothic",
                   "Microsoft Sans Serif"
            };
            listStyleFont = new string[] 
            {
                    "Regular",
                    "Bold",
                    "Italic",
                    "Strikeout",
                    "Underline"  
            };
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
